// @ts-nocheck
import * as cornerstone from "cornerstone-core"
import cornerstoneTools from "cornerstone-tools"
import * as dicomParser from "dicom-parser"
import cornerstoneMath from "cornerstone-math"
import Hammer from "hammerjs"
import cornerstoneWADOImageLoader from "cornerstone-wado-image-loader"
import cornerstoneWebImageLoader from 'cornerstone-web-image-loader'


// import cornerstoneSideImageLoader from "./cornerstone-side-image-loader";
import { imageLoaders } from "@/utils/imageLoaders"
import {getOS} from "@/utils/window";

cornerstone.registerImageLoader("blobImgLoader", imageLoaders.blobImgLoaderNew) //for blob getMPRSnapshot
cornerstone.registerImageLoader("blobLoader", imageLoaders.blobLoader) //for blob getSingleDCMImage
cornerstone.registerImageLoader("blobLoaderMip", imageLoaders.blobLoaderMip) //for blob rendering_engine/mip2

cornerstoneTools.external.cornerstone = cornerstone
cornerstoneTools.external.Hammer = Hammer
cornerstoneTools.external.cornerstoneMath = cornerstoneMath

cornerstoneTools.disableLogger()

cornerstoneTools.init({
  globalToolSyncEnabled: true,
  autoResizeViewports: true,
  showSVGCursors: true // attention: mouseButtonMask must include 1
})
// 指定要注册加载程序的基石实例
cornerstoneWebImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.cornerstone = cornerstone
cornerstoneWADOImageLoader.external.dicomParser = dicomParser
cornerstoneWADOImageLoader.external.cornerstoneMath = cornerstoneMath

// cornerstoneSideImageLoader.external.cornerstone = cornerstone

const { isWin, systemInfo } = getOS()
// cornerstoneWADOImageLoader.webWorkerManager.initialize({
//   maxWebWorkers: (isWin && systemInfo !== 'Win10') ? 1 : navigator.hardwareConcurrency ? Math.max(navigator.hardwareConcurrency - 2, 1) : 1,
//   startWebWorkersOnDemand: false,
//   taskConfiguration: {
//     decodeTask: {
//       initializeCodecsOnStartup: false,
//       usePDFJS: false,
//       strict: false
//     }
//   }
// })

// Set the tool font and font size
const fontFamily = "Work Sans, Roboto, OpenSans, HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif"
cornerstoneTools.textStyle.setFont(`16px ${fontFamily}`)
// Set the tool width
cornerstoneTools.toolStyle.setToolWidth(1)
// Set color for inactive tools
cornerstoneTools.toolColors.setToolColor("#fbb03b")
// Set color for active tools
cornerstoneTools.toolColors.setActiveColor("rgb(0, 255, 0)")
//快速翻层
// cornerstoneTools.addTool(cornerstoneTools.StackScrollTool)
// cornerstoneTools.setToolActive('StackScroll', { mouseButtonMask: 1 });
// 鼠标滚轮事件
// cornerstoneTools.setToolActive('StackScrollMouseWheel', {})//默认开启
// 缩放
cornerstoneTools.addTool(cornerstoneTools.ZoomTool, {
  configuration: {
    invert: false,
    preventZoomOutsideImage: false,
    minScale: 0.05,
    maxScale: 20.0
  }
})

// 放大镜
// cornerstoneTools.addTool(cornerstoneTools.MagnifyTool)
// 移动
cornerstoneTools.addTool(cornerstoneTools.PanTool)
// 调节对比度
cornerstoneTools.addTool(cornerstoneTools.WwwcTool)
//尺子 长度
cornerstoneTools.addTool(cornerstoneTools.LengthTool)
//矩形
cornerstoneTools.addTool(cornerstoneTools.RectangleRoiTool)
//自由画笔
cornerstoneTools.addTool(cornerstoneTools.FreehandRoiTool)




// 旋转
// cornerstoneTools.addTool(cornerstoneTools.RotateTool)

const configuration = {
  handleRadius: 5,
  drawHandlesOnHover: true,
  hideHandlesIfMoving: false
}
// 框
// cornerstoneTools.addTool(cornerstoneTools.RectangleRoiTool, {
//   configuration
// })
// 角度
// cornerstoneTools.addTool(cornerstoneTools.AngleTool, {
//   configuration
// })
// 正圆
// cornerstoneTools.addTool(cornerstoneTools.CircleRoiTool, {
//   ...configuration,
//   hideHandlesIfMoving: true
// })
// 椭圆
// cornerstoneTools.addTool(cornerstoneTools.EllipticalRoiTool, {
//   ...configuration,
//   hideHandlesIfMoving: true
// })
//标尺
// cornerstoneTools.addTool(cornerstoneTools.ScaleOverlayTool, {
//   configuration: {
//     minorTickLength: 10,
//     majorTickLength: 15,
//     verticalLine: { start: { x: 0, y: 0 }, end: { x: 0, y: 0 } }
//   }
// })

// 默认开启
// cornerstoneTools.setToolActive('ScaleOverlay')

// 开启覆盖模式
cornerstoneTools.setToolActive("BaseOverLayer");

//预加载
cornerstoneTools.stackPrefetch.setConfiguration({
  preserveExistingPool: true,
  maxImagesToPrefetch: 1000 //Infinity
})
